package o;

import android.content.pm.PackageInfo;
import android.os.Build.VERSION;
import android.util.ArraySet;
import androidx.annotation.NonNull;
import com.huawei.appgallery.updatemanager.impl.keysets.dao.AppKeySets;
import com.huawei.appgallery.updatemanager.impl.keysets.dao.AppKeySets.KeySetBean;
import java.io.File;
import java.security.PublicKey;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class das {
    private static das ˎ;
    private final Map<String, AppKeySets> ˏ = new HashMap();

    public static synchronized das ˎ() {
        das o_das;
        synchronized (das.class) {
            if (ˎ == null) {
                ˎ = new das();
            }
            o_das = ˎ;
        }
        return o_das;
    }

    public das() {
        for (AppKeySets appKeySets : dar.ˏ().ˋ()) {
            this.ˏ.put(appKeySets.ˋ(), appKeySets);
        }
    }

    public static List<String> ˋ(PackageInfo packageInfo) {
        if (VERSION.SDK_INT < 21 || packageInfo == null) {
            return null;
        }
        AppKeySets appKeySets = (AppKeySets) ˎ().ˏ.get(packageInfo.packageName);
        if (packageInfo.applicationInfo.sourceDir == null) {
            return null;
        }
        long lastModified = new File(packageInfo.applicationInfo.sourceDir).lastModified();
        if (appKeySets == null || appKeySets.ˎ() != lastModified) {
            return ˎ(packageInfo);
        }
        if (appKeySets.ˊ() == null) {
            return null;
        }
        return appKeySets.ˊ().ˏ();
    }

    private static ArrayList<String> ˎ(@NonNull PackageInfo packageInfo) {
        AppKeySets appKeySets = new AppKeySets();
        appKeySets.ˋ(packageInfo.versionCode);
        appKeySets.ˊ(packageInfo.packageName);
        File file = new File(packageInfo.applicationInfo.sourceDir);
        if (!file.exists()) {
            return null;
        }
        appKeySets.ˏ(file.lastModified());
        e ˎ = dap.ˎ(file);
        if (ˎ.ˎ == null || ˎ.ˎ.isEmpty() || ˎ.ˊ == null || ˎ.ˊ.isEmpty()) {
            ˏ(appKeySets);
            return null;
        }
        List arrayList = new ArrayList();
        for (Entry entry : ˎ.ˎ.entrySet()) {
            if (ˎ.ˊ.contains((String) entry.getKey())) {
                Iterator it = ((ArraySet) entry.getValue()).iterator();
                while (it.hasNext()) {
                    arrayList.add(eux.ॱ(((PublicKey) it.next()).getEncoded()));
                }
            }
        }
        KeySetBean keySetBean = new KeySetBean();
        keySetBean.ˎ(arrayList);
        appKeySets.ॱ(keySetBean);
        ˏ(appKeySets);
        return arrayList;
    }

    private static void ˏ(AppKeySets appKeySets) {
        ˎ().ˏ.put(appKeySets.ˋ(), appKeySets);
        dar.ˏ().ˎ(appKeySets);
    }

    public void ˋ() {
        if (VERSION.SDK_INT >= 20) {
            for (PackageInfo ˋ : ((cdg) cdb.ॱ(cdg.class)).ʻ()) {
                ˋ(ˋ);
            }
        }
    }
}
